package com.cndw.kungfu.model.pvp;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.goods.domain.SubPops;
import com.cndw.kungfu.model.goods.domain.SysModeAwards;

@Component
public class ChallengeManager {
	/**
	 * 获取敌人列表
	 * @param ranking
	 * @return
	 */
	public List<Map<String, Object>> getEnmeys(long playerId, int ranking){
		List<Map<String, Object>> enemys = new LinkedList<Map<String, Object>>();
		if(ranking == 0){
			//第一次挑战NPC
			Map<String, Object> npcEnemy = new HashMap<String, Object>();
			long npcPlayerId = ConstantsModel.challengeNpcPlayerId;
			int npcRanking = DataSerialize.getInstance().getChallengeData().getLastRanking() + 1;
			npcEnemy.put("ranking", npcRanking);
			int npcLevel = Context.getCacheCenter().getMonsterById(ConstantsModel.challengeNpcMId).getLevel();
			npcEnemy.put("level", npcLevel);
			npcEnemy.put("playerId", npcPlayerId);
			npcEnemy.put("vocationId", ConstantsModel.challengeNpcVId );
			String npcName = Context.getCacheCenter().getMonsterById(ConstantsModel.challengeNpcMId).getName();
			npcEnemy.put("playerName", npcName );
			SysModeAwards awards = SysModeAwards.get(getAwardsId(npcRanking, npcLevel));
			List<SubPops> subPopsList = awards != null ? awards.getSubPopsList() : null;
			npcEnemy.put("awards", Context.getGoodsServer().makeAwards(subPopsList));
			enemys.add(npcEnemy);
		}else{
			Set<Integer> enemyRanking = enemyRanking(ranking);
			for(int i : enemyRanking){
				long eId = DataSerialize.getInstance().getChallengeData().getPlayer(i);
				if(eId != 0 && eId != playerId){
					Map<String, Object> tempMap = new HashMap<String, Object>();
					Player enemy = Context.getPlayerServer().getByPlayerId(eId);
					tempMap.put("ranking", i);
					tempMap.put("level", enemy.getLevel());
					tempMap.put("playerId", eId);
					tempMap.put("vocationId", enemy.getVocationId());
					tempMap.put("playerName", enemy.getPlayerName());
					SysModeAwards awards = SysModeAwards.get(getAwardsId(i, enemy.getLevel()));
					List<SubPops> subPopsList = awards != null ? awards.getSubPopsList() : null;
					tempMap.put("awards", Context.getGoodsServer().makeAwards(subPopsList));
					enemys.add(tempMap);
				}
			}
		}
		return enemys;
	}
	
	/**
	 * 获取武林大会可挑战敌人名次
	 * @param ranking
	 * @return
	 */
	public Set<Integer> enemyRanking(int ranking){
		Set<Integer> rankingSet = new LinkedHashSet<Integer>();
		if(ranking > 0){
			if(ranking <= ConstantsModel.challengeEnemyNum){
				 int index = ConstantsModel.challengeEnemyNum + 1;
				 while(index > 0){
					 if(index != ranking){
						 rankingSet.add(index);
					 }
					 index--;
				 }
			 }else if(ranking <= 100){
				 int distance = 1;
					if(ranking > 50){
						distance = 10;
					}
					for(int i = ranking - distance; i < ranking && i >= ranking - distance * ConstantsModel.challengeEnemyNum && i > 0; i -= distance){
						rankingSet.add(i);
					}
			 }else{
				 rankingSet.add(ranking -1);
				 rankingSet.add(ranking - ranking / 20);
				 rankingSet.add(ranking - ranking / 10);
				 rankingSet.add(ranking - ranking / 5);
				 rankingSet.add((int)(ranking - ranking / 1.5));
			 }
		}
		return rankingSet;
	}
	
	/**
	 * 获取奖励id
	 */
	public int getAwardsId(int ranking, int level){
		int awardsId = 140000 + (ranking - 1) * 10;
		if(ranking <= 5){
			awardsId += level / 10 - 1;
		}
		return awardsId;
	}
}
